use std::mem::MaybeUninit;

use jni::objects::{GlobalRef, JClass};

use crate::error::AppErr;

use super::global::get_java_vm;



pub const IO_EXCEPTION: &'static str = "java/io/IOException";

pub struct GlobalClass {
    pub login_info: GlobalRef,
}

static mut GLOBAL_CLASS: MaybeUninit<GlobalClass> = MaybeUninit::uninit();

pub fn init() -> Result<(), AppErr> {
    let mut env = get_java_vm().attach_current_thread()?;

    let login_info = env.find_class("rustlib/service/LoginInfo")?;

    let clz = GlobalClass {
        login_info: env.new_global_ref( login_info )?,
    };

    unsafe { GLOBAL_CLASS.write(clz); }

    Ok(())
}

fn global_class() -> &'static GlobalClass {
    unsafe { GLOBAL_CLASS.assume_init_ref() }
}

pub fn login_info_class() -> JClass<'static> {
    unsafe {
        JClass::from_raw( global_class().login_info.as_raw() )
    }
}










